<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
	<script type="text/javascript" src="scripts/boot.js"></script>
	<style type="text/css">
	    .errorText
	    {
	        color:red;
	    }
    </style>
</head>
<body>
	<form id="form1" method="post">
        <div style="padding-left:30px;padding-bottom:15px;padding-top: 15px;">
       	<input name="id" class="mini-hidden" />
       	<input id="medicine_id" class="mini-hidden" />
       	<input name="recallId" class="mini-hidden" />
        <input name="warehouseId" class="mini-hidden" />
            <table style="width:100%;" cellspacing="0" cellpadding="10">
                <tr>
                    <td style="width:80px;">药品名称：</td>
                    <td style="width:150px;">
    					<input name="medicineId" valueFromSelect="true" class="mini-autocomplete" required="true" style="width:180px;" valueField="id" textField="name" url="storage_detail_find_medicine.do" emptyText="请输入药品拼音首字母搜索药品"/>
    				</td>
    				<td id="medicineId_error" class="errorText" ></td>
                </tr>
                <tr>
                    <td style="width:80px;">数量：</td>
                    <td>
                    	<input id="medicine_quantity" name="quantity" minValue="1" maxValue="99999" class="mini-spinner" style="width:180px;"/>
                    </td>
                </tr>
            </table>
        </div>
        <div style="text-align:center;padding-top:30px;">               
            <a class="mini-button" onclick="onOk" style="width:60px;margin-right:50px;">确定</a>       
            <a class="mini-button" onclick="onCancel" style="width:60px;">取消</a>       
        </div> 
    </form>

<script type="text/javascript">
	mini.parse();
	//验证药品是否已经添加
	var mId = mini.get("medicine_id");
	var medicine = mini.getbyName("medicineId");
	var warehouseId = mini.getbyName("warehouseId");
	
	var form = new mini.Form("#form1");
	
	//保存数据
	function SaveData() {
	    var o = form.getData(true); 
	    var quantity = mini.get("medicine_quantity");
	    var quantityValue = quantity.getFormValue();
	    var medicineValue = mini.getbyName("medicineId").getFormValue();
	    var medicine = mini.getbyName("medicineId");
	    form.validate();
    	if (form.isValid() == false) return;
    	//验证药品是否有2个以上已经添加
	    $.post("recall_detail_check_medicine_e.do",{recallId: mini.getbyName("recallId").getFormValue(), medicineId: medicine.getFormValue()},function(data){
			if(data.able){
				//验证药品数量是否充足
				$.post("recall_detail_check_inventory.do",{medicineId: medicineValue, quantity: quantityValue, warehouseId: warehouseId.getFormValue()},
		        		function(data){
			        		if(data.able){
			        			$.ajax({
			        		        url: "recall_detail_save_edit.do",
			        				type: 'post',
			        				dataType: 'json',
			        		        data: o,
			        		        cache: false,
			        		        success: function (text) {
			        		            if(text.success){
			        		            	window.CloseOwnerWindow("succ");
			        		            }else{
			        		            	mini.alert("数据错误！");
			        		            }
			        		        }
			        		    });
				            }else{
				            	mini.alert("该药品库存量不足！");
				            	quantity.setValue("1");
				            }
		        },"json");
            }else{
            	mini.alert("所选药品已存在列表中！");
            }
		},"json");
	}
	
	//标准方法接口定义
    function SetData(data) {
        if (data.action == "edit") {
            //跨页面传递的数据对象，克隆后才可以安全使用
            data = mini.clone(data);
            warehouseId.setValue(data.warehouseId);
            $.ajax({
                url: "recall_detail_find.do?recallDetail.id=" + data.id,
                type: 'post',
                cache: false,
                dataType: "json",
                success: function (text) {
                	form.setData(text.recallDetail);
                	medicine.setText(text.recallDetail.medicineName);
                	mId.setValue(text.recallDetail.medicineId);
                    form.setChanged(false);
                }
            });
        }
    }
    
    function CloseWindow(action) {            
        if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
        else window.close();            
    }
	
    function onOk(e) {
        SaveData();
    }
    
    function onCancel(e) {
        CloseWindow("cancel");
    }

</script>  
</body>
</html>